/*
 * Copyright (c) 2018.
 * This file was written by Fancyears·Milos·Malvis @connor. Any question/bug you can't post to 1243093366@qq.com.
 * ALL RIGHTS RESERVED.
 */
package org.fancyears.renderer.jtable;

import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.AbstractCellEditor;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author: Fancyears·Milos·Malvis
 * @CreateDate: 2018年12月4日 下午3:20:13
 * @Description:
 */

public class HyperRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, HyperlinkListener {
	private static final long serialVersionUID = 1L;
	private LocalPanel deleterPane = null;
	private LocalPanel updatePane = null;
	private JPanel panel;
	private JTable table;

	public HyperRenderer(JTable table) {
		this.table = table;
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		deleterPane = new LocalPanel();
		deleterPane.setContentType("text/html");
		deleterPane.setText(" <a href='' style='margin-top:-5px;color:red';padding-buttom:5px >删除</a>");
		deleterPane.setName("delete");
		deleterPane.setEditable(false);
		deleterPane.setBackground(panel.getBackground());
		deleterPane.addHyperlinkListener(this);
		updatePane = new LocalPanel();
		updatePane.setContentType("text/html");
		updatePane.setText(" <a href='' style='margin-top:-5px'>修改</a>");
		updatePane.setName("update");
		updatePane.setEditable(false);
		updatePane.setBackground(panel.getBackground());
		updatePane.addHyperlinkListener(this);
		panel.add(deleterPane);
		panel.add(updatePane);
	}

	@Override
	public Object getCellEditorValue() {
		return null;
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
			int row, int column) {
		deleterPane.setRowNum(row);
		deleterPane.setColumnNum(column);
		updatePane.setRowNum(row);
		updatePane.setColumnNum(column);
		return panel;
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		deleterPane.setRowNum(row);
		deleterPane.setColumnNum(column);
		updatePane.setRowNum(row);
		updatePane.setColumnNum(column);
		return panel;
	}

	@Data
	@EqualsAndHashCode(callSuper = false)
	class LocalPanel extends JEditorPane {
		private static final long serialVersionUID = 1L;
		private Integer rowNum;
		private Integer columnNum;
	}

	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			DefaultTableModel model = null;
			LocalPanel pane = (LocalPanel) e.getSource();
			if (table.getModel() instanceof DefaultTableModel) {
				model = (DefaultTableModel) table.getModel();
			}
			switch (pane.getName()) {
			case "delete":
				int confirmResult = JOptionPane.showConfirmDialog(null, "确定删除 :" + "?", "路径删除",
						JOptionPane.YES_NO_OPTION); // 删除操作
				if (confirmResult == 0) { // 列表中移除
					int row = pane.getRowNum();
					int column = pane.getColumnNum();
					// 正常情况下删除选中的行数
					if (null != model.getValueAt(row, column)) {
						model.removeRow(row);
					}
					// 如果选中倒数第二行之后紧接着要删最后一行，如果不进行判断的话则会报异常
					else if (null != model.getValueAt(row - 1, column)) {
						model.removeRow(row);
					}
				}
				break;
			case "update":
				int confirmResult2 = JOptionPane.showConfirmDialog(null, "修改?", "路径删除", JOptionPane.YES_NO_OPTION); // 删除操作
				if (confirmResult2 == 0) { // 列表中移除
				}
				break;
			}
		}
	}
}
